<?


 
function count_period($date1,$date2) {


    /**
     * function：计算两个日期相隔多少年，多少月，多少天
     * param string $date1[格式如：2011-11-5]
     * param string $date2[格式如：2012-12-01]
     * return array array('年','月','日');
     */

        $datetime1 = new \DateTime($date1);
        $datetime2 = new \DateTime($date2);
        $interval = $datetime1->diff($datetime2);
        $time['y']         = $interval->format('%Y');
        $time['m']         = $interval->format('%m');
        $time['d']         = $interval->format('%d');
        $time['h']         = $interval->format('%H');
        $time['i']         = $interval->format('%i');
        $time['s']         = $interval->format('%s');
        $time['a']         = $interval->format('%a');    // 两个时间相差总天数


        return array($time['y'], $time['m'], $time['d'], $time['h'], $time['i'], $time['s'], $time['a']);
        //return $time;





 /*



 
 
	
	if (strtotime($startdate) > strtotime($enddate)) { 
        $ymd = $enddate; 
        $enddate = $startdate; 
        $startdate = $ymd; 
    } 






    list($y1, $m1, $d1) = explode('-', $startdate); 
    list($y2, $m2, $d2) = explode('-', $enddate); 
    $y = $m = $d = $_m = 0; 

   $math = ($y2 - $y1) * 12 + $m2 - $m1;



//round 是要处理四舍五入的哦。
//echo $y = round($math / 12);

//floor计算年份才能正确。因为大于0.5的话，就会计算多一年了。
    $y = floor($math / 12);
    $m = intval($math % 12);
    $d = (mktime(0, 0, 0, $m2, $d2, $y2) - mktime(0, 0, 0, $m2, $d2, $y2)) / 86400;
    if ($d < 0) { 
        $m -= 1; 
        $d += date('j', mktime(0, 0, 0, $m2, 0, $y2)); 
    } 
    $m < 0 && $y -= 1; 
	
    return array($y, $m, $d);

 */
 
}
?>